package my.controller;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import my.bean.Cart;
import my.bean.Order;
import my.bean.Tea;
import my.bean.User;
import my.service.ICartService;
import my.service.IOrderService;
import my.service.ITeaService;
import my.service.IUserService;
import my.service.impl.ICartServiceImpl;
import my.service.impl.IOrderServiceImpl;
import my.service.impl.ITeaServiceImpl;
import my.service.impl.IUserServiceImpl;

@Controller
@RequestMapping("/user")
public class UserController {
	IUserService userService = new IUserServiceImpl();
	ICartService cartService = new ICartServiceImpl();
	IOrderService orderService = new IOrderServiceImpl();
	ITeaService teaService = new ITeaServiceImpl();

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request, String account, String password) {
		ModelAndView modelAndView = new ModelAndView();
		User user = userService.login(account, password);
		if (user != null) {
			modelAndView = findAllTeaMess(request);
			modelAndView.addObject("user", user);
			request.getSession().setAttribute("user", user);
		} else {
			modelAndView.addObject("loginMessage", "登录失败!");
			modelAndView.setViewName("login.jsp");
		}
		return modelAndView;
	}

	@RequestMapping("/register")
	public ModelAndView register(HttpServletRequest request, String account, String pwd1, String pwd2, String email,
			String phone) {
		ModelAndView modelAndView = new ModelAndView();
		User user = new User(account, pwd1, email, phone, new Date());

		if (userService.findByAccount(account) != null) {
			modelAndView.addObject("errorMessage", "账号重复!请输入新的账号!");
			modelAndView.setViewName("register.jsp");
			return modelAndView;
		} else if (!pwd1.equals(pwd2)) {
			modelAndView.addObject("errorMessage", "密码不相同!请重新出入密码!");
			modelAndView.setViewName("register.jsp");
		} else {
			try {
				int record = userService.register(user);
				if (record > 0) {
					modelAndView.setViewName("login.jsp");
					request.getSession().setAttribute("user", user);
				}
			} catch (SQLException e) {
				modelAndView.addObject("errorMessage", "发生错误!");
				modelAndView.setViewName("register.jsp");
				return modelAndView;
			}
		}
		return modelAndView;
	}

	@RequestMapping("/baseMessageModify")
	public ModelAndView baseMessageModify(HttpServletRequest request, String email, String phone) {
		ModelAndView modelAndView = new ModelAndView();
		User user = (User) request.getSession().getAttribute("user");
		user.setEmail(email);
		user.setPhone(phone);
		try {
			userService.modifyBaseMessage(user);
			request.getSession().setAttribute("user", user);
			modelAndView.addObject("user", user);
			modelAndView.setViewName("userMessage.jsp");
			return modelAndView;
		} catch (SQLException e) {
			modelAndView.addObject("errorMessage", "修改失败!");
			modelAndView.setViewName("userMessage.jsp");
			return modelAndView;
		}
	}

	@RequestMapping("/pwdModify")
	public ModelAndView pwdModify(HttpServletRequest request, String oldPassword, String newPassword, String confirm) {
		ModelAndView modelAndView = new ModelAndView();
		int id = ((User) request.getSession().getAttribute("user")).getId();
		String thispwd = ((User) request.getSession().getAttribute("user")).getPassword();
		if (!oldPassword.equals(thispwd)) {
			modelAndView.addObject("errorMessage", "原密码不正确!");
			modelAndView.setViewName("changePwd.jsp");
		} else if (!newPassword.equals(confirm)) {
			modelAndView.addObject("errorMessage", "两次输入的密码不匹配!");
			modelAndView.setViewName("changePwd.jsp");
		} else {
			try {
				userService.modifyPasswordById(id, newPassword);
				User user = userService.findByUserId(id);
				request.getSession().setAttribute("user", user);
				modelAndView.addObject("user", user);
				modelAndView.setViewName("userMessage.jsp");

			} catch (SQLException e) {
				modelAndView.addObject("errorMessage", "修改失败!");
				modelAndView.setViewName("changePwd.jsp");
				return modelAndView;
			}
		}
		return modelAndView;
	}

	@RequestMapping("/findAllTeaMess")
	public ModelAndView findAllTeaMess(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Tea> list = teaService.findAll();
		modelAndView.addObject("teas", list);
		modelAndView.setViewName("teasManager.jsp");
		return modelAndView;
	}

	@RequestMapping("/teaMessage")
	public ModelAndView teaMessage(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		Tea tea = teaService.findTeaById(id);
		modelAndView.addObject("tea", tea);
		modelAndView.setViewName("teaMessage.jsp");
		return modelAndView;
	}

	@RequestMapping("/addCartMessage")
	public ModelAndView addCartMessage(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		Tea tea = teaService.findTeaById(id);
		modelAndView.addObject("tea", tea);
		modelAndView.setViewName("addCartMessage.jsp");
		return modelAndView;
	}

	@RequestMapping("/addCart")
	public ModelAndView addCart(HttpServletRequest request, int id, double count) {
		ModelAndView modelAndView = new ModelAndView();
		User user = (User) request.getSession().getAttribute("user");
		Tea tea = teaService.findTeaById(id);
		Cart cart = new Cart(user, tea, count, new Date());
		try {
			cartService.addCart(cart);
			modelAndView = getCartList(request);
		} catch (SQLException e) {
			modelAndView.addObject("errorMessage", "加入购物车失败!");
			modelAndView.setViewName("cartList.jsp");
			return modelAndView;
		}
		return modelAndView;
	}

	@RequestMapping("/getCart")
	public ModelAndView getCart(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		Cart cart = cartService.findByCartId(id);
		Tea tea = teaService.findTeaById(cart.getTea().getId());
		modelAndView.addObject("tea", tea);
		modelAndView.addObject("cart", cart);
		modelAndView.setViewName("setCartMessage.jsp");
		return modelAndView;
	}

	@RequestMapping("/setCartMessage")
	public ModelAndView setCartMessage(HttpServletRequest request, int id, double count) {
		ModelAndView modelAndView = new ModelAndView();
		Cart cart = cartService.findByCartId(id);
		cart.setCount(count);
		try {
			cartService.modifyCart(cart);
			modelAndView = getCartList(request);
		} catch (SQLException e) {
		}
		return modelAndView;
	}

	@RequestMapping("/getCartList")
	public ModelAndView getCartList(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		int id = ((User) request.getSession().getAttribute("user")).getId();
		List<Cart> carts = cartService.findByUserid(id);
		modelAndView.addObject("carts", carts);
		modelAndView.setViewName("cartList.jsp");
		return modelAndView;
	}

	@RequestMapping("/getOrderList")
	public ModelAndView getOrderList(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		int id = ((User) request.getSession().getAttribute("user")).getId();
		List<Order> orders = orderService.findByUserid(id);
		modelAndView.addObject("orders", orders);
		modelAndView.setViewName("orderList.jsp");
		return modelAndView;
	}

	@RequestMapping("/buy")
	public ModelAndView buy(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		User user = (User) request.getSession().getAttribute("user");
		Cart cart = cartService.findByCartId(id);
		Tea tea = teaService.findTeaById(cart.getTea().getId());
		if (tea.getStock() < cart.getCount()) {
			modelAndView.addObject("errorMessage", "库存不足!请重新核对数量");
			cart.getTea().setStock(tea.getStock());
			modelAndView.addObject("tea", tea);
			modelAndView.setViewName("cartMessage.jsp");
			return modelAndView;
		} else {
			Order order = new Order(user, tea, cart.getCount(), new Date());
			try {
//				cartService.deleteCart(id);
//				orderService.addOrder(order);
				tea.setStock(tea.getStock() - cart.getCount());
//				teaService.modifyTeaMessage(tea);
				userService.buy(id, order, tea);
				modelAndView = getOrderList(request);
				return modelAndView;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return modelAndView;
	}

	@RequestMapping("/cartDelete")
	public ModelAndView cartDelete(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			cartService.deleteCart(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		modelAndView = getCartList(request);
		return modelAndView;
	}

	@RequestMapping("/getOrder")
	public ModelAndView getOrder(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		Order order = orderService.findByOrderId(id);
		Tea tea = teaService.findTeaById(order.getTea().getId());
		modelAndView.addObject("order", order);
		modelAndView.addObject("tea", tea);
		modelAndView.setViewName("orderMessage.jsp");
		return modelAndView;
	}
	
	@RequestMapping("/reset")
	public ModelAndView reset(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("redirect:/user/login.jsp");
		request.getSession().removeAttribute("user");
		return modelAndView;
	}
}